package com.my.test.web.invest_order;

import com.my.test.service.invest_order.model.InvestOrder;
import com.my.test.service.invest_order.service.InvestOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/order")
public class InvestOrderCtl {
    private static final Logger LOGGER = LoggerFactory.getLogger(InvestOrderCtl.class);

    @Autowired
    private InvestOrderService investOrderService;

    @RequestMapping(value = "/batch_insert")
    public String batchInsert(){
        String filePath = InvestOrderCtl.class.getClassLoader().getResource("data/order.csv").getPath();
        System.out.println(filePath);
        File csv = new File(filePath);
        BufferedReader br = null;
        String line = "";
        String everyLine = "";
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        List<InvestOrder> orderList = new ArrayList<>();
        try {
            br = new BufferedReader(new FileReader(csv));
            while ((line = br.readLine()) != null) { //读取到的内容给line变量
                everyLine = line;
                String[] arr = everyLine.split(",");
                InvestOrder io = new InvestOrder();
                io.setUid(Long.valueOf(arr[0]));
                io.setCreateDate(format.parse(arr[1]));
                orderList.add(io);
                //System.out.println(everyLine);
            }
            investOrderService.insertByBatch(orderList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/person/index";
    }
}